package server.commonData;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import server.ServerEngin;
import server.ServerTransportProxy;
import server.messageId.S2CMessageId;
import server.msgBean.LoginMessage.RoomInfo;
import server.msgBean.PlayMessage.QuiteRoomRes;

import com.core.action.TimeAction;
import com.core.initialize.Message;
import com.core.interfaces.IScheduledThread;

/**
 * 房间
 * @author King
 *
 */
public class Room extends TimeAction
{
	/**房间名称**/
	private String roomName;
	/**基础倍率**/
	private int beilv;
	
	
	public Room(String roomName,int beilv)
	{
		super(100);
		this.roomName = roomName;
		this.beilv = beilv;
	}

	private IScheduledThread executor;
	
	/**桌子集合**/
	private Map<Long, Table> talbes = new HashMap<Long, Table>();
	
	public void init()
	{
		this.executor = ServerEngin.getInstance().getExecutor().pushAction(this);
	}
	
	/**加入游戏**/
	public Table joinTable(Player player)
	{
		Table table =  null;
		for(Table temp:talbes.values())
		{
			if(temp.isFull())
				continue;
			table = temp;
			break;
		}
		if(table==null)
		{
			table = TableFactory.createTable(2,this);
			talbes.put(table.getId(), table);
		}
		//通知桌上的玩家某某进入了
		if(!table.isEmpty())
		{
			Message msg = Message.newMessage();
			msg.setModuleAction(S2CMessageId.Login.MODULE_ID, S2CMessageId.Login.JOIN_TABLE_NOTIFY_RES);
			msg.setBody(player.showTablePlayerInfo());
			ServerTransportProxy.sendMsgByUser(msg, table.getUsers());
		}
		table.addPlayer(player);
		return table;
	}
	
	/**
	 * 加入桌子
	 * @param table
	 * @param player
	 */
	public void joinTable(Table table,Player player)
	{
		//通知桌上的玩家某某进入了
		if(!table.isEmpty())
		{
			Message msg = Message.newMessage();
			msg.setModuleAction(S2CMessageId.Login.MODULE_ID, S2CMessageId.Login.JOIN_TABLE_NOTIFY_RES);
			msg.setBody(player.showTablePlayerInfo());
			ServerTransportProxy.sendMsgByUser(msg, table.getUsers());
		}
		table.addPlayer(player);
	}
	
	/**
	 * 离开桌子
	 * @param player
	 */
	public void leftTable(Player player)
	{
		Table table = player.getTable();
		table.delPlayer(player);
		boolean hasPlayer = false;
		//计算房间内玩家数量 如果没有了 则删除房间
		for(PokerPlayer pokerPlayer:table.getPlayers().values())
		{
			if(pokerPlayer.getPlayer().getType()==Player.USER)
			{
				hasPlayer= true;
				break;
			}
		}
		if(hasPlayer)
		{
			Message msg = Message.newMessage();
			QuiteRoomRes.Builder builder = QuiteRoomRes.newBuilder();
			builder.setUserId(player.getId());
			msg.setModuleAction(S2CMessageId.PLAY.MODULE_ID, S2CMessageId.PLAY.QUITE_ROOM_RES);
			msg.setBody(builder);
			ServerTransportProxy.sendMsgByUser(msg, table.getUsers());
			return;
		}else
		{
			table.over();
		}
	}
	
	/**
	 * 获得房间所在的线程池
	 * @return
	 */
	public IScheduledThread getExecutor() {
		return executor;
	}

	@Override
	public void executeOnTime() throws Exception
	{
		List<Table> tables = new ArrayList<Table>(this.talbes.values());
		long curMill = getCurTime();
		for(Table table:tables)
		{
			if(table.isOver())
			{
				this.talbes.remove(table.getId());
				continue;
			}
			
			table.onTime(curMill);	
		}
	}
	
	/**
	 * 显示房间信息给客户端
	 * @param builder
	 */
	public void showRoomInfo(RoomInfo.Builder builder)
	{
		//TODO
	}
	
	/**
	 * 获取桌子
	 * @param tableId
	 * @return
	 */
	public Table getTable(long tableId)
	{
		return this.talbes.get(tableId);
	}

	public String getRoomName() {
		return roomName;
	}

	public int getBeilv() {
		return beilv;
	}
}
